﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADNDNetwork.Netowork.MessageContainer;
using System.Threading;



namespace DungeonAndDragonApplication.TestHarnness
{
    public partial class Form1 : Form
    {
        private readonly SynchronizationContext syncContext;
        private Thread updateMessage;
        private delegate void AddMessageDelegate(object item);


        public Form1()
        {
            InitializeComponent();
            syncContext = new SynchronizationContext();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {

            DNDMessage m = new DNDMessage("testUser", tbSendMessage.Text, "archille", null, DateTime.Now);
            MessageBus.GetInstance().SendMessage(m);

            this.updateMessage =
               new Thread(new ThreadStart(this.UpdateUI));
            this.updateMessage.Start();
        }

        private void UpdateUI()
        {
            while (true)
            {
                if (MessageBus.GetInstance().GetLatestReceivedMessage() == null)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                    continue;
                }

                string s = MessageBus.GetInstance().GetLatestReceivedMessage().MessageValue;
                MessageBus.GetInstance().EmptyReceivedMessageQueue();

                tbMessage.Text += s.ToString();
            }
        }
    }
}

